package org.study.swing.study01.component;

import lombok.Data;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

/**
 * Java Swing组件：组合框
 * 参考 https://blog.csdn.net/hitmengfanchao/article/details/137005911
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年11月13日
 */
public class ComboBoxDemo2 {
    public static void main(String[] args) {
        // test01();
        test02();
    }

    public static void test01() {
        Province[] provinces = {
                new Province(11, "北京"),
                new Province(23, "黑龙江"),
                new Province(22, "吉林"),
                new Province(21, "辽宁")
        };
        JFrame frame = new JFrame("示例：组合框");

        JPanel panel = new JPanel();
        JTextField textField = new JTextField();
        // 创建组合框
        JComboBox<Province> comboBox = new JComboBox<>(provinces);
        panel.add(comboBox);
        frame.add(panel, BorderLayout.CENTER);
        frame.add(textField, BorderLayout.NORTH);
        // 在下拉列表的末尾增加新的选项
        comboBox.addItem(new Province(37, "山东"));
        // 在下拉列表的第1个位置增加新的选项
        comboBox.insertItemAt(new Province(31, "上海"), 1);
        // 注册事件监听器（匿名类）
        comboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                // 获取事件源
                JComboBox jcb = (JComboBox) event.getSource();
                // 返回当前选中项
                Province province = (Province) jcb.getSelectedItem();
                // 在文本框中显示选中项
                textField.setText(province.getCode() + "-" + province.getName());
            }
        });
        // 设置当前选中项为第2项
        comboBox.setSelectedIndex(2);

        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void test02() {
        JFrame frame = new JFrame("示例：组合框");

        JPanel panel = new JPanel();
        JTextField textField = new JTextField();
        // 创建组合框的数据模型
        DefaultComboBoxModel<Province> model = new DefaultComboBoxModel<>();
        model.addElement(new Province(11, "北京"));
        model.addElement(new Province(31, "上海"));
        model.addElement(new Province(23, "黑龙江"));
        model.addElement(new Province(22, "吉林"));
        model.addElement(new Province(21, "辽宁"));
        model.addElement(new Province(37, "山东"));
        // 创建组合框
        JComboBox<Province> comboBox = new JComboBox<>(model);
        panel.add(comboBox);
        frame.add(panel, BorderLayout.CENTER);
        frame.add(textField, BorderLayout.NORTH);
        // 注册事件监听器（匿名类）
        comboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                // 返回数据模型中被选中的元素
                Province province = (Province) model.getSelectedItem();
                // 在文本框中显示选中项
                textField.setText(province.getCode() + "-" + province.getName());
            }
        });
        // 设置当前选中项为数据模型中的第2项
        model.setSelectedItem(model.getElementAt(2));

        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    @Data
    static class Province implements Serializable {
        private static final long serialVersionUID = 1L;

        private int code;
        private String name;

        public Province(int code, String name) {
            this.code = code;
            this.name = name;
        }

        @Override
        public String toString() {
            return this.name;
        }
    }
}
